package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * 好友服务接口实现类
 */
@Service
public class FriendApiImpl implements FriendApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存好友关系 2条记录
     * @param friend
     */
    @Override
    public void saveFriend(Friend friend) {
        Long userId = friend.getUserId(); //当前用户id
        Long friendId = friend.getFriendId();//好友id
        //1.先根据userId friendId查询好友表记录是否存在
        Query query1 = new Query();
        query1.addCriteria(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        //2.不存在 再保存
        if(!mongoTemplate.exists(query1,Friend.class)){
            Friend f1 = new Friend();
            f1.setUserId(friend.getUserId());
            f1.setFriendId(friend.getFriendId());
            f1.setCreated(System.currentTimeMillis());//添加好友时间
            mongoTemplate.save(f1);
        }
        //3.先根据friendId userId 查询好友表记录是否存在
        Query query2 = new Query();
        query2.addCriteria(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        //4.不存在 再保存
        if(!mongoTemplate.exists(query2,Friend.class)){
            Friend f2 = new Friend();
            f2.setUserId(friend.getFriendId());
            f2.setFriendId(friend.getUserId());
            f2.setCreated(System.currentTimeMillis());//添加好友时间
            mongoTemplate.save(f2);
        }
    }

    /**
     * 分页查询联系人列表功能
     * @param page
     * @param pagesize
     * @param currentUserId
     * @return
     */
    @Override
    public PageResult<Friend> queryFriendPage(int page, int pagesize, Long currentUserId) {
        //1.查询联系人列表总记录数
        Query query = new Query();
        query.limit(pagesize).skip((page - 1) * pagesize);
        query.addCriteria(Criteria.where("userId").is(currentUserId));//根据当前用户id查询好友ids
        long counts = mongoTemplate.count(query, Friend.class);
        //2.查询当前页面要展示的数据
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        //3.将数据封装返回
        long pages = (counts / pagesize) + (counts%pagesize > 0 ? 1:0);
        return new PageResult<>(counts,(long)pagesize,pages,(long)page,friendList);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     */
    @Override
    public Long count(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query,Friend.class);
    }
    /**
     * 检查是否是好友关系
     * @param currentUserId
     * @param focusId
     * @return
     */
    @Override
    public Friend checkIfYouFriend(Long currentUserId, Long focusId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(currentUserId).and("friendId").is(focusId));
        return  mongoTemplate.findOne(query,Friend.class);
    }
    /**
     * 删除好友的双向关系
     * @param friend
     */
    @Override
    public void deleteFriend(Friend friend) {
        Query query = new Query();
        Query query2 = new Query();
        query.addCriteria(Criteria.where("userId").is(friend.getUserId()).and("friendId").is(friend.getFriendId()));
        query.addCriteria(Criteria.where("userId").is(friend.getFriendId()).and("friendId").is(friend.getUserId()));
        mongoTemplate.remove(query,Friend.class);
        mongoTemplate.remove(query2,Friend.class);
    }
}
